package com.ecube.mqtt.utils;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class PropertyUtil {

    public static String getProperties(String key, String fileName) throws IOException {
        Properties prop = new Properties();


        //读取属性文件a.properties
        FileInputStream fis = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedReader reader = new BufferedReader (new InputStreamReader(bis));
        //InputStream in = new BufferedInputStream(new FileInputStream("src/main/java/com/ecube/mqtt/utils/charge.properties"));

        prop.load(reader);   ///加载属性列表

//        Iterator<String> it=prop.stringPropertyNames().iterator();

//        while(it.hasNext()){
//
//            String key=it.next();
//
//            System.out.println(key+":"+prop.getProperty(key));
//
//        }
        String property = prop.getProperty(key);
        reader.close();

        return property;


    }

    public static void setProperty(Map<String, String> propmap, String fileName) throws IOException {
        ///保存属性到b.properties文件
        Properties prop = new Properties();

        File file = new File(fileName);
        OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
        //FileOutputStream oFile = new FileOutputStream("src/main/java/com/ecube/mqtt/utils/charge1.properties", false);//true表示追加打开
        for (Map.Entry<String, String> entry : propmap.entrySet()) {
            prop.setProperty(entry.getKey(), entry.getValue());
        }
        prop.store(oStreamWriter,"Data update");

        oStreamWriter.close();
    }

    public static void main(String[] args) throws IOException {
        //System.out.println(getProperties("env"));
        PropertyUtil property = new PropertyUtil();
        File f = new File(property.getClass().getResource("/").getPath());
        System.out.println(f);

    }
}
